package cn.hist.hfp.service;

import cn.hist.hfp.converter.CommentConverter;
import cn.hist.hfp.entity.Comment;
import cn.hist.hfp.entity.Stray;
import cn.hist.hfp.entity.User;
import cn.hist.hfp.exception.ResourceNotFoundException;
import cn.hist.hfp.repository.CommentRepository;
import cn.hist.hfp.vo.CommentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class CommentService {

    @Value(value = "${hfp.page-size.comment}")
    private int pageSize;

    private final
    CommentRepository commentRepository;

    private final
    CommentConverter commentConverter;

    private final
    StrayService strayService;

    private final UserService userService;

    @Autowired
    public CommentService(CommentRepository commentRepository, CommentConverter commentConverter, StrayService strayService, UserService userService) {
        this.commentRepository = commentRepository;
        this.commentConverter = commentConverter;
        this.strayService = strayService;
        this.userService = userService;
    }

    public Comment create(Comment comment) {
        return commentRepository.save(comment);
    }

    public Comment findOne(int id) {
        Optional<Comment> optionalComment = commentRepository.findById(id);
        if (optionalComment.isPresent()) {
            return optionalComment.get();
        }
        throw new ResourceNotFoundException();
    }

    public List<CommentVO> findSomeByStray(int page, int stray_id) {
        Stray stray = strayService.findOne(stray_id);
        return commentConverter.covert(
                commentRepository.findAllByStray(stray, PageRequest.of(page, pageSize))
                        .getContent());
    }

    public List<CommentVO> findSomeByUser(int page, int user_id) {
        User user = userService.findOne(user_id);
        return commentConverter.covert(
                commentRepository.findAllByUser(user, PageRequest.of(page, pageSize))
                        .getContent());
    }

    public int numberOfPagesSortByStray(int stray_id) {
        Stray stray = strayService.findOne(stray_id);
        int number = commentRepository.findAllByStray(stray, PageRequest.of(0, pageSize)).getTotalPages();
        return number <= 0 ? 1 : number;
    }

    public int numberOfPagesSortByUser(int user_id) {
        User user = userService.findOne(user_id);
        int number = commentRepository.findAllByUser(user, PageRequest.of(0, pageSize)).getTotalPages();
        return number <= 0 ? 1 : number;
    }

    public Comment update(Comment comment, int user_id) {
        Optional<Comment> optionalComment = commentRepository.findById(comment.getId());
        if (optionalComment.isPresent()) {
            Comment storedComment = optionalComment.get();
            if (storedComment.getUser().getId() == user_id)
                return commentRepository.save(comment);
        }
        return comment;
    }

    public int delete(int id, int user_id) {
        Optional<Comment> optionalComment = commentRepository.findById(id);
        if (optionalComment.isPresent()) {
            Comment comment = optionalComment.get();
            if (comment.getUser().getId() == user_id)
                commentRepository.delete(comment);
        }
        return commentRepository.findById(id).isPresent() ? 0 : 1;
    }
}
